/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.lnu.ums.dal.impl;

import edu.lnu.ums.dal.IConcurrentAccessGenericDAO;
import edu.lnu.ums.dal.exception.OldVersionSavingException;
import edu.lnu.ums.entities.core.AbstractEntity;
import javax.persistence.EntityManager;
import org.hibernate.StaleObjectStateException;


public abstract class AbstractConcurrentAccessGenericDAO<T extends AbstractEntity> extends AbstractGenericDAO<T> implements IConcurrentAccessGenericDAO<T>{

    public AbstractConcurrentAccessGenericDAO(Class<T> cls) {
        super(cls);
    }

    @Override
    public T merge(T obj) throws OldVersionSavingException {
        EntityManager em = obj.getFactory().createEntityManager();
        em.getTransaction().begin();
        try {
            T t = em.merge(obj);
            em.getTransaction().commit();
            return t;
        } catch (StaleObjectStateException ex) {
            em.getTransaction().rollback();
            throw new OldVersionSavingException(ex);
        }
    }
}
